<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js数组随机排序</title>
	</head>
	<body>
		<h1 id="a1"></h1>
		<h1 id="a2"></h1>
		<script type="text/javascript">
			// 1、两两对比，最后一个数在最后出现可能性较大
			var arr1 = [1, 2, 3, 4, 5];
			arr1.sort(function(a, b) {
				return 0.5 - Math.random();
			});
			document.querySelector('#a1').innerHTML = "第一种：" + arr1;
			console.log(arr1);
			//2、洗牌算法（推荐）
			var arr2 = [1, 2, 3, 4, 5];
			var len = arr2.length;
			console.log('len', len);
			for (var i = 0; i < len; i++) {
				var index = Math.floor(Math.random() * len);
				var temp = arr2[index];
				arr2[index] = arr2[i];
				arr2[i] = temp;
			}
			document.querySelector('#a2').innerHTML = "第二种：" + arr2;
			console.log(arr2);
		</script>

	</body>
</html>
